package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.NumStrEnum;
import com.softer.wxzj.common.util.ShiroUserUtil;
import com.softer.wxzj.entity.ResourceTree;
import com.softer.wxzj.entity.SysRole;
import com.softer.wxzj.mapper.RoleMapper;
import com.softer.wxzj.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public CommonResult insert(SysRole role) {
        int count = roleMapper.getCountByRoleCode(role.getCode());
        if (0 < count) {
            return CommonResult.strResult(0, "编码重复！");
        }
        if (!("333").equals(role.getType())){
          role.setType("0"+role.getType());
        }
        role.setCreater(ShiroUserUtil.getUserName());
        int i = roleMapper.insert(role);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public CommonResult update(SysRole role) {
        if (!("333").equals(role.getType())){
          role.setType(("0")+role.getType());
        }
        int i = roleMapper.update(role);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public CommonResult delete(String id) throws DataIntegrityViolationException {
        int i = roleMapper.delete(id);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public PageInfo<SysRole> getList(Page page, SysRole role) {
        //只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询（Select）方法会被分页。
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<SysRole> list = roleMapper.getList(role);
        PageInfo<SysRole> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<SysRole> getListByType(String type) {
        SysRole role = new SysRole();
        role.setState(NumStrEnum.ONE.getNum());
        role.setType(type);
        List<SysRole> list = roleMapper.getListByType(role);
        return list;
    }

    @Override
    public SysRole getInfo(String id) {
        SysRole role= roleMapper.getInfo(id);
        if (!("333").equals(role.getType())){
            role.setType(role.getType().substring(1));
        }
        return role;
    }

    @Override
    public List<ResourceTree> getResourceTree(String type) {
        String resourceId;
        //角色类型 1/2/3/4/5/6/8/9 为外网角色，10为内网角色
        resourceId = roleMapper.getResourceIdByCorpNo(ShiroUserUtil.getUserName());
        List<ResourceTree> resourceList = roleMapper.getResourceTree(NumStrEnum.ZERO.getNum(), resourceId);
        if (null != resourceList && resourceList.size() > 0) {
            dealList(resourceList,resourceId);
            return resourceList;
        }
        return new ArrayList<>();
    }

    private void dealList(List<ResourceTree> resourceList, String resourceId) {
        if (null != resourceList && resourceList.size() > 0) {
            for (ResourceTree r : resourceList) {
                List<ResourceTree> children = roleMapper.getResourceTree(r.getId(), resourceId);
                r.setChildren(children);
                dealList(children, resourceId);
            }
        }
    }

    @Override
    public PageInfo<SysRole> fuzzySearch(Page page, String fuzzy) {
        //只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询（Select）方法会被分页。
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<SysRole> list = roleMapper.fuzzySearch(fuzzy);
        PageInfo<SysRole> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

}
